<html>
  <body>
    <h1>sqlite-vec demo/simple-wasm</h1>

    <div id="target"></div>
    <script type="module">
      import {default as init} from "https://cdn.jsdelivr.net/npm/sqlite-vec-wasm-demo@latest/sqlite3.mjs";

      function log(msg) {
        const pre = document.querySelector('#target').appendChild(document.createElement("pre"));
        pre.textContent = msg;
      }

      const sqlite3 = await init();
      const db = new sqlite3.oo1.DB(":memory:");

      const [sqlite_version, vec_version] = db.selectArray('select sqlite_version(), vec_version();')
      log(`sqlite_version=${sqlite_version}, vec_version=${vec_version}`);

      const items = [
        [1, [0.1, 0.1, 0.1, 0.1]],
        [2, [0.2, 0.2, 0.2, 0.2]],
        [3, [0.3, 0.3, 0.3, 0.3]],
        [4, [0.4, 0.4, 0.4, 0.4]],
        [5, [0.5, 0.5, 0.5, 0.5]],
      ];
      const query = [0.3, 0.3, 0.3, 0.3];

      db.exec('CREATE VIRTUAL TABLE vec_items USING vec0(embedding float[4]);');
      const stmt = db.prepare("INSERT INTO vec_items(rowid, embedding) VALUES (?, ?)");
      for(const item of items) {
        stmt
        .bind(1, item[0])
        .bind(2, new Float32Array(item[1]).buffer)
        .stepReset();
      }
      stmt.finalize();

      const rows = db
        .selectArrays(
          `
        SELECT
          rowid,
          distance
        FROM vec_items
        WHERE embedding MATCH ?
        ORDER BY distance
        LIMIT 3
      `, new Float32Array(query).buffer
        );

      log(JSON.stringify(rows));
    </script>
  </body>
</html>
`
